package com.fengye.algorithms.huawei;

import java.util.*;

/**
 * https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0?tpId=37&tqId=21325&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
 * @Author fengye
 * @Date 2023/4/7 11:34
 **/
public class CharStatisMain {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<Character, Integer> map = new TreeMap<>(new Comparator<Character>() {
            @Override
            public int compare(Character o1, Character o2) {
                return o2.compareTo(o1);
            }
        });
        while (in.hasNextLine()) {
            String n = in.nextLine();
            char[] chars = n.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                Character c = chars[i];
                if (map.containsKey(c)) {
                    map.put(c, Integer.valueOf(map.get(c).intValue() + 1));
                } else {
                    map.put(c, Integer.valueOf(1));
                }
            }
            ArrayList<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
                @Override
                public int compare(Map.Entry<Character, Integer> o1,
                                   Map.Entry<Character, Integer> o2) {
                    if (o2.getValue() > o1.getValue()) {
                        return 1;
                    } else if (o1.getValue().intValue() == o2.getValue().intValue()) {
                        return  o1.getKey().compareTo(o2.getKey());
                    }
                    return -1;
                }
            });
            for (Map.Entry<Character, Integer> entry : list) {
                System.out.print(entry.getKey());
            }
            System.out.println();
        }

    }
}
